﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QRCode.Models;

namespace QRCode.Controllers
{
    public class CouponController : Controller
    {
        private CouponDBContext db = new CouponDBContext();

        //
        // GET: /Coupon/

        [Authorize]
        public ActionResult Index()
        {
            return View(db.Coupons.ToList());
        }

        //
        // GET: /Coupon/Details/5

        [Authorize]
        public ActionResult Details(int id = 0)
        {
            Coupon coupon = db.Coupons.Find(id);
            if (coupon == null)
            {
                return HttpNotFound();
            }
            return View(coupon);
        }

        //
        // GET: /Coupon/Create

        [Authorize]
        public ActionResult Create(String qr)
        {

            Coupon newCoupon = new Coupon();

            int SalesPromotionID;
            string SalesPromotionName = "";
            int PromotionTypeID;
            string PromotionTypeName = "";

            SalesPromotionController cc = new SalesPromotionController();
            List<SalesPromotion> SalesPromotions = cc.db.SalesPromotions.Where(b => b.Code == qr).ToList();

            SalesPromotionID = SalesPromotions[0].ID;
            SalesPromotionName = SalesPromotions[0].Name;
            PromotionTypeID = SalesPromotions[0].PromotionTypeID;

            PromotionTypeController pt = new PromotionTypeController();
            List<PromotionType> PromotionTypes = pt.db.PromotionTypes.Where(b => b.ID == PromotionTypeID).ToList();

            PromotionTypeName = PromotionTypes[0].Name;

            newCoupon.SalesPromotionID = SalesPromotionID;

            if (User != null)
                newCoupon.UserName = User.Identity.Name;
            else
                newCoupon.UserName = "QRTest";

            SalesPromotionQuestionController sp = new SalesPromotionQuestionController();
            List<SalesPromotionQuestion> SalesPromotionQuestions = sp.db.SalesPromotionQuestions.Where(b => b.SalesPromotionID == SalesPromotionID).ToList();

            ViewBag.SalesPromotionName = SalesPromotionName;
            ViewBag.PromotionTypeName = PromotionTypeName;
            ViewBag.SalesPromotionQuestions = SalesPromotionQuestions;

            return View(newCoupon);
        }

        //
        // POST: /Coupon/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Create(Coupon coupon)
        {
            if (ModelState.IsValid)
            {
                //Get answers 
                String answer1 = Request.Form["q1"];
                String answer2 = Request.Form["q2"];
                String answer3 = Request.Form["q3"];
                String answer4 = Request.Form["q4"];

      
                DateTime saveNow = DateTime.Now;

                coupon.Activated = saveNow;
                coupon.Used = saveNow;

                db.Coupons.Add(coupon);
                db.SaveChanges();

                //Save answers
                SalesPromotionAnswer answer = new SalesPromotionAnswer();
                answer.CouponID = coupon.ID;
                answer.Answer1 = answer1;
                answer.Answer2 = answer2;
                answer.Answer3 = answer3;
                answer.Answer4 = answer4;
                answer.Active = true;

                SalesPromotionAnswerController con = new SalesPromotionAnswerController();
                con.db.SalesPromotionAnswers.Add(answer);
                con.db.SaveChanges();

                if (User.Identity.Name == "admin")
                    return RedirectToAction("Index");
                else
                    return RedirectToAction("Index", "Home", null);

            }

            return View(coupon);
        }

        //
        // GET: /Coupon/Edit/5

        [Authorize]
        public ActionResult Edit(int id = 0)
        {
            Coupon coupon = db.Coupons.Find(id);
            if (coupon == null)
            {
                return HttpNotFound();
            }
            return View(coupon);
        }

        //
        // POST: /Coupon/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Edit(Coupon coupon)
        {
            if (ModelState.IsValid)
            {
                db.Entry(coupon).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(coupon);
        }

        //
        // GET: /Coupon/Delete/5

        [Authorize]
        public ActionResult Delete(int id = 0)
        {
            Coupon coupon = db.Coupons.Find(id);
            if (coupon == null)
            {
                return HttpNotFound();
            }
            return View(coupon);
        }

        //
        // POST: /Coupon/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult DeleteConfirmed(int id)
        {
            Coupon coupon = db.Coupons.Find(id);
            db.Coupons.Remove(coupon);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

}